package TestCase;
import org.junit.*;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import pagelib.*;


public class CredoTest {
	
	static final String shopSmartPhomesLink="//DIV[@id='TR']/DIV[1]/A";
	static final String shopCameraPhonesLink="//DIV[@id='TR']/DIV[2]/A";
	static final String shopMessagingPhonesLink="//DIV[@id='TR']/DIV[3]/A";
	static final String shopTouchscreenPhonesLink="//DIV[@id='TR']/DIV[4]/A";
	static final String allPhonesLink="//DIV[@id='TR']/A/H2";
	
	static final String phoneSlide1Link="//DIV[@id='TL']/OL[@id='slideShowCount']/LI[@id='slide0']/SPAN";
	static final String phoneSlide2Link="//DIV[@id='TL']/OL[@id='slideShowCount']/LI[@id='slid1']/SPAN";	
	static final String phoneSlide3Link="//DIV[@id='TL']/OL[@id='slideShowCount']/LI[@id='slide2']/SPAN";
	static final String phoneSlide4Link="//DIV[@id='TL']/OL[@id='slideShowCount']/LI[@id='slide3']/SPAN";
	
	//links for phone types tab
	static final String allPhonesTabLink = "//LI[@id='allPhonesTab']/A";
	static final String messagingTabLink="//LI[@id='msgPhonesTab']/A";
	static final String cameraTabLink = "//LI[@id='cameraPhonesTab']/A";
	static final String trouchscreenTabLink = "//LI[@id='touchScreenTab']/A";
	static final String smartphonesTabLink = "//LI[@id='smartPhonesTab']/A";
	static final String freePhoneTabLink = "//LI[@id='freePhonesTab']/A";
	
	//links for 1 phone from each phone type
	static final String allPhone1Link = "//DIV[@id='versionA']/DIV[1]/DIV[2]/DIV[1]/A";
	static final String smartPhone1Link="//DIV[@id='mainContentWrapper']/DIV[2]/DIV[2]/DIV[@id='versionA']/DIV[1]/DIV[2]/DIV[1]/A";
	static final String cameraPhone1Link="//DIV[@id='mainContentWrapper']/DIV[2]/DIV[2]/DIV[@id='versionA']/DIV[1]/DIV[2]/DIV[1]/A";
	static final String messagePhone1Link="//DIV[@id='mainContentWrapper']/DIV[2]/DIV[2]/DIV[@id='versionA']/DIV[1]/DIV[2]/DIV[1]/A";	
	static final String freePhone1Link="//DIV[@id='mainContentWrapper']/DIV[2]/DIV[2]/DIV[@id='versionA']/DIV[1]/DIV[2]/DIV[1]/A"; 

	
	CredoHome testCredoHome = new CredoHome();	
	PhoneTypesPage p = new PhoneTypesPage();
	Plans myPlan = new Plans();
	
@Before
	public void setUp() throws Exception{
		testCredoHome.openTest();
	}

@After 
public void tearDown() throws Exception{
	//testCredoHome.closeTest();
	}

@Test
public void checkTopMenu()
	{
		String menu = "link=Phones";
	}

//smart phones, camera phones, flip phones,and free phones links hsould be there
@Test
public void clickPhonesLink()
	{	
		testCredoHome.openHome();
	 	testCredoHome.clickMenuItem("link=Phones");
		assertTrue(testCredoHome.checkIfLinkPresent(shopSmartPhomesLink));
		assertTrue(testCredoHome.checkIfLinkPresent(shopCameraPhonesLink));
		assertTrue(testCredoHome.checkIfLinkPresent(shopMessagingPhonesLink));
		assertTrue(testCredoHome.checkIfLinkPresent(shopTouchscreenPhonesLink));
		assertTrue(testCredoHome.checkIfLinkPresent(allPhonesLink));
		assertTrue(testCredoHome.checkIfLinkPresent(phoneSlide1Link));
		assertTrue(testCredoHome.checkIfLinkPresent(phoneSlide2Link));
		assertTrue(testCredoHome.checkIfLinkPresent(phoneSlide2Link));
		assertTrue(testCredoHome.checkIfLinkPresent(phoneSlide2Link));
}

@Test
public void checkCoverage()
	{
		String zipCodeID = "//INPUT[@id='coverageMapsZip']";
		String goButtonLink = "//DIV[@id='coverageMaps']/FIELDSET/A[@id='covMap']";
		String zipCode = "91789";
		p.enterZip(zipCodeID, zipCode);
		p.clickVisibleLink(goButtonLink);
	}
@Test
public void clickAllPhonesPage()
	{
		testCredoHome.openHome();
		testCredoHome.clickSubMenu("link=Phones","link=all phones");
		testCredoHome.isElementVisible(allPhone1Link);
	}

//click on smart phones link and check if the phones present
@Test
public void clickSmartPhoneLink()
	{
		testCredoHome.openHome();
		testCredoHome.clickSubMenu("link=Phones","link=smartphones");
		testCredoHome.isElementVisible(smartPhone1Link);
	}


//click on camera link and check if all camera phones are present on the page
@Test
public void clickCameraPhoneLink()
	{
		testCredoHome.openHome();
		testCredoHome.clickSubMenu("link=Phones","link=camera");
		testCredoHome.isElementVisible(cameraPhone1Link);
		System.out.println(testCredoHome.getElementText(cameraPhone1Link));
	}

@Test
public void clickMessagePhoneLink()
	{
		testCredoHome.openHome();
		testCredoHome.checkSubMenuPresent("link=Phones", "link=messaging");
		testCredoHome.isElementVisible(messagePhone1Link);
		//System.out.println(testCredoHome.getElementText("//DIV[@id='mainContentWrapper']/DIV[2]/DIV[2]/DIV[@id='versionA']/DIV[1]/DIV[2]/DIV[1]/A"));
	}

//Click on free link and check if all free phones are showing onthe page
@Test
public void clickFreePhoneLink()
{
	testCredoHome.openHome();
	testCredoHome.clickSubMenu("link=Phones","link=free");
	testCredoHome.isElementVisible(freePhone1Link);
}

//Click on Plans link and check if links for all, Individual, family, business plans are there
//Also check if there are three tabs on the slide show in the center of the page
@Test
public void clickPlansLink()
{
	testCredoHome.openHome();
	testCredoHome.clickMenuItem("link=Plans");
	assertTrue(testCredoHome.checkIfLinkPresent("//DIV[@id='TR']/DIV[1]/A"));
	assertTrue(testCredoHome.checkIfLinkPresent("//DIV[@id='TR']/DIV[2]/A"));
	assertTrue(testCredoHome.checkIfLinkPresent("//DIV[@id='TR']/DIV[3]/A"));
	assertTrue(testCredoHome.checkIfLinkPresent("//DIV[@id='TR']/A/H2"));
	assertTrue(testCredoHome.checkIfLinkPresent("//DIV[@id='TL']/OL[@id='slideShowCount']/LI[@id='slide0']/SPAN"));
	assertTrue(testCredoHome.checkIfLinkPresent("//DIV[@id='TL']/OL[@id='slideShowCount']/LI[@id='slide1']/SPAN"));
	assertTrue(testCredoHome.checkIfLinkPresent("//DIV[@id='TL']/OL[@id='slideShowCount']/LI[@id='slide2']/SPAN"));
}

@Test
public void clickAllPlansLink()
{	
	testCredoHome.openHome();
	testCredoHome.clickSubMenu("link=Plans", "link=all plans");
	System.out.println(testCredoHome.getTitle());
	//assertEquals("Cell Phone Plans &; Cell Phones Plan Comparison : CREDO Mobile",testCredoHome.getTitle());
}

@Test
public void clickIndividualPlansLink()
{
	testCredoHome.openHome();
	testCredoHome.clickSubMenu("link=Plans","link=individual plans");
	assertTrue(myPlan.checkTabExist());
	assertTrue(myPlan.checkAllIndLinksExist());
}

@Test
public void clickFamilyPlanLink()
{
	testCredoHome.openHome();
	testCredoHome.clickSubMenu("link=Plans","link=family plans");
	assertTrue(myPlan.checkTabExist());
}

@Test
public void clickBusinessPlansLink()
{
	testCredoHome.openHome();
	testCredoHome.clickSubMenu("link=Plans","link=business plans");
	assertTrue(myPlan.checkTabExist());
	assertTrue(myPlan.checkAllBusinessLinksExist());
}

@Test
public void showPhoneDetails()
	{
		testCredoHome.openHome();
		testCredoHome.clickSubMenu("link=Phones","link=all phones");
		p.clickOnPhone("//DIV[@id='versionA']/DIV[1]/DIV[2]/DIV[1]/A");
	}


@Test
public void showPhonePrice()
	{
		testCredoHome.openHome();
		testCredoHome.clickSubMenu("link=Phones","link=all phones");
		System.out.println(p.getPhonePrice("//DIV[@id='versionA']/DIV[1]/DIV[2]/DIV[3]/SPAN"));
	}


@Test
public void AddPhoneToTheCart()
	{	
		String addToCartLinkID ="//DIV[@id='versionA']/DIV[1]/DIV[2]/DIV[4]/A";
		String priceID = "//DIV[@id='versionA']/DIV[1]/DIV[2]/DIV[3]/SPAN";
		String zipTextBoxID="//DIV[@id='mainContentWrapper']/DIV/DIV[2]/DIV/DIV/DIV/INPUT";
		String goButtonLink ="//DIV[@id='mainContentWrapper']/DIV/DIV[2]/DIV/DIV/DIV/INPUT[2]";
		String messageID = "//DIV[@class='messagingbox']/DIV[2]/DIV";
		String priceOfSelectedPhone, priceOfSelectedPhoneInCart;
		String messageText;
		
		testCredoHome.openHome();
		testCredoHome.clickSubMenu("link=Phones","link=all phones");
		
		//get the price of the selected phone
		priceOfSelectedPhone = p.getPhonePrice(priceID);
		//click add to cart button
		p.clickAddPhoneToCart(addToCartLinkID);
		//enter zip code and click on 'go' button
		p.enterZip(zipTextBoxID,"91789");
			//click go button
		p.clickVisibleLink(goButtonLink);
		//get price of phone from cart
		priceOfSelectedPhoneInCart = p.getPhonePrice("//TABLE[@id='cartDetails']/TBODY/TR[4]/TD[2] ");
		assertEquals(priceOfSelectedPhone,priceOfSelectedPhoneInCart);
		messageText = p.getMessage(messageID);
		System.out.println(messageText);
	}



}
